package org.microblog.service.impl;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.microblog.dao.BlogDao;
import org.microblog.entity.Blog;
import org.microblog.entity.BlogVo;
import org.microblog.service.BlogService;
import org.microblog.util.Page;
import org.microblog.util.SessionFatoryUtil;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogServiceImpl implements BlogService {
    SqlSessionFactory sessionFactory = SessionFatoryUtil.sessionFactory;
    BlogVo vo=new BlogVo();
    @Override
    public Blog getBlogById(int id) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        BlogDao dao=sqlSession.getMapper(BlogDao.class);
        Blog blog=dao.findBlogById(id);
        sqlSession.commit();
        sqlSession.close();
        return blog;
    }

    @Override
    public int getForwardCountByBid(int bid) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        BlogDao dao=sqlSession.getMapper(BlogDao.class);
        int ret=dao.getForwardCountByBid(bid);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public int deleteBlog(int id) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession(true);
        BlogDao dao=sqlSession.getMapper(BlogDao.class);
        int ret=dao.deleteBlog(id);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public int getBlogCount(int uId) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        BlogDao dao=sqlSession.getMapper(BlogDao.class);
        int ret=dao.getBlogCount(uId);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public void getAllBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession(true);
        vo.setuId(uId);
        vo.setKeyword(keyword);
        vo.setPageNo(pageObj.getBeginIndex());
        vo.setPageSize(pageObj.getPageSize());

        BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
        int totalCount = blogDao.getAllBlogsCount(vo);
        pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
        if (totalCount > 0) {
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Blog> blogList = blogDao.getPopBlogsByPage(vo);
            pageObj.setBlogsList(blogList);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setBlogsList(new ArrayList<>());
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public int addNewBlog(Blog blog) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        BlogDao dao=sqlSession.getMapper(BlogDao.class);
        int ret=dao.addNewBlog(blog);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }

    @Override
    public void getPopBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession(true);
        vo.setuId(uId);
        vo.setKeyword(keyword);
        vo.setPageNo(pageObj.getBeginIndex());
        vo.setPageSize(pageObj.getPageSize());
        BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
        int totalCount = blogDao.getPopBlogsCount(vo);
        pageObj.setTotalCount(totalCount);
        if (totalCount > 0) {
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Blog> blogList = blogDao.getPopBlogsByPage(vo);
            pageObj.setBlogsList(blogList);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setBlogsList(new ArrayList<>());
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void getMyBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession(true);
        vo.setuId(uId);
        vo.setKeyword(keyword);
        vo.setPageNo(pageObj.getBeginIndex());
        vo.setPageSize(pageObj.getPageSize());

        BlogDao blogDao = sqlSession.getMapper(BlogDao.class);
        int totalCount = blogDao.getBlogCountByUidAndKeyword(vo);
        pageObj.setTotalCount(totalCount);
        if (totalCount > 0) {
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Blog> blogList = blogDao.getPopBlogsByPage(vo);
            pageObj.setBlogsList(blogList);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setBlogsList(new ArrayList<>());
        }
        sqlSession.commit();
        sqlSession.close();
    }


    @Override
    public int forwardBlog(int uId, String content, int forwardBlogId) throws SQLException {
        SqlSession sqlSession=sessionFactory.openSession();
        BlogDao dao=sqlSession.getMapper(BlogDao.class);
        int ret=dao.forwardBlog(uId, content, forwardBlogId);
        sqlSession.commit();
        sqlSession.close();
        return ret;
    }
}
